package com.step.ems.pages.wb

import android.content.Context
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import com.step.ems.Destinations.ID
import com.step.ems.R
import com.step.ems.api.ApiService
import com.step.ems.api.WbStatus
import com.step.ems.api.res.WbCompleteRes
import com.step.ems.api.res.WbDetailRes
import com.step.ems.common.UiState
import com.step.ems.common.launch
import com.step.ems.pages.common.UploadViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class WbDetailViewModel @Inject constructor(
    @ApplicationContext context: Context,
    savedStateHandle: SavedStateHandle,
    override val apiService: ApiService
) : UploadViewModel(context, apiService) {
    val id = savedStateHandle.get<Int>(ID) ?: 0
    val workSituationIndex: MutableIntState = mutableIntStateOf(-1)

    private val _uiState = MutableStateFlow<UiState>(UiState.Empty)
    val uiState = _uiState.asStateFlow()

    private val _data = MutableStateFlow(WbDetailRes())
    val data get() = _data.asStateFlow()

    var description: MutableState<String> = mutableStateOf("")

    init {
        getData()
    }

    private fun getData() {
        launch(
            state = _uiState,
            delayTime = 200,
            onStart = {
                _uiState.value = UiState.Loading()
            }) {
            val res = apiService.wbDetail(id).get()
            _data.value = res
            description.value = res.maintenanceOrder.description
            workSituationIndex.intValue = res.maintenanceOrder.workingCondition
            setImages(res.maintenanceFiles)
            _uiState.value = UiState.Success
        }
    }

    fun changeStatus() {
        launch(
            state = _uiState,
            onStart = {
                _uiState.value = UiState.Loading()
            }) {
            val order = _data.value.maintenanceOrder
            when (order.maintenanceStatus) {
                WbStatus.Created.value -> {
                    apiService.wbSignIn(order).get()
                }

                WbStatus.CheckedIn.value -> {
                    val req = WbCompleteRes()
                    order.workingCondition = workSituationIndex.intValue
                    if (order.workingCondition < 0) {
                        throw Exception(context.getString(R.string.wbAreaTip))
                    }
                    order.description = description.value
                    if (order.description.isBlank()) {
                        throw Exception(context.getString(R.string.siteNoteTip))
                    }
                    if (imageList.isEmpty()) {
                        throw Exception(context.getString(R.string.sitePhotosTip))
                    }
                    req.maintenanceOrder = order
                    req.filePathList = imageList
                    apiService.wbComplete(req).get()
                }
            }
            _uiState.value = UiState.Success
            getData()
        }
    }
}